package View;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

import Controller.Tasker;
import Date.ArrangeView;
import Events.GenericEvent;


public class GenericFrame extends JFrame implements ActionListener
{
	private JLabel label_background,label_header, label_footer, label_content,label_menu, label_view;
	private JMenuItem menuItemRestore;
    private JMenuItem menuItemClose;
    private JPopupMenu popupContextual;
    private JSeparator separator;
    private SystemTray tray;
    private final TrayIcon trayIcon;
    private Image image_tray;
    private JButton close, minimize;
    private Vector<JButton> tags;
    private String [] labels = new String[5];
    private String dir = "images/";
    private int category=0;
    private ContentView content ;
    private Color gris = new Color(204,204,204);
    private Tasker task;
    private ArrangeView arra;

	
	public GenericFrame(Tasker task)
	{
		image_tray = Toolkit.getDefaultToolkit().getImage( "tasker_icon.png"); 
	    trayIcon = new TrayIcon(image_tray, "Tasker", null); 
	    this.task = task;
	    arra=task.getArrangeView();
	    
	    labels[0]= "ALL";
	    labels[1]= "CALENDAR";
	    labels[2]= "ALARM";
	    labels[3]= "TASK";
	    labels[4]= "SETTINGS";
	    
		createFrame();	
		createTray();
	}
	
	
		
	public void createFrame()
	{
		setUndecorated(true);
		//setLocation(300,200);
		setSize(1205,700);
		getContentPane().setBackground(gris);
		setLayout(new FlowLayout(FlowLayout.CENTER,0,0));	
		
		
		label_background = new JLabel();
		label_background.setPreferredSize(new Dimension(1200,700));
		label_background.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		
		getContentPane().add(label_background);
		createTemplate();
		createHeader();
		createContent();
		setVisible(true);
	}
	
	public void createTray()
	{
		popupContextual = new JPopupMenu();
        menuItemRestore = new JMenuItem();
        separator = new JSeparator();
        menuItemClose = new JMenuItem();

        menuItemRestore.setText("Show Tasker");
        menuItemRestore.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                menuItemRestoreActionPerformed(evt);
            }
        });
        
        popupContextual.add(menuItemRestore);
        popupContextual.add(separator);

        menuItemClose.setText("Exit");
        menuItemClose.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                menuItemCloseActionPerformed(evt);
            }
        });
        popupContextual.add(menuItemClose);

        addWindowStateListener(new java.awt.event.WindowStateListener() 
        {
            public void windowStateChanged(java.awt.event.WindowEvent evt)
            {
                changeEvent(evt);
            }
        });

       
	}
	
	
	public void createTemplate()
	{
		label_header = new JLabel();
		label_content = new JLabel();
		label_footer = new JLabel();
		
		label_header.setPreferredSize(new Dimension(1200,100));
		label_header.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		
		label_content.setPreferredSize(new Dimension(1200,550));
		label_content.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
		
		label_footer.setPreferredSize(new Dimension(1200,50));		
		
		label_background.add(label_header);
		label_background.add(label_content);
		label_background.add(label_footer);
		label_background.setIcon(new ImageIcon(dir+"background.png"));
		
	}
	
	public void createHeader()
	{
		JLabel label_buttons = new JLabel();
		label_buttons.setPreferredSize(new Dimension(1200,50));
		label_buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));
		
		close = new JButton();
		minimize = new JButton();
		
		close.setPreferredSize(new Dimension(50,30));
		close.setActionCommand("CLOSE");
		close.addActionListener(this);
		close.setIcon(new ImageIcon(dir+"close.png"));
		close.setContentAreaFilled(false);
		close.setBorderPainted(false);
		close.setFocusPainted(false);
				
		minimize.setPreferredSize(new Dimension(50,30));
		minimize.setActionCommand("MINIMIZE");
		minimize.addActionListener(this);
		minimize.setIcon(new ImageIcon(dir+"minimize.png"));
		minimize.setContentAreaFilled(false);
		minimize.setBorderPainted(false);
		minimize.setFocusPainted(false);
		
		label_buttons.add(minimize);
		label_buttons.add(close);
		label_header.add(label_buttons);
	}
	
	public void createContent()
	{
		JLabel label_tag = new JLabel();
		label_tag.setPreferredSize(new Dimension(200,550));
		label_tag.setLayout(new FlowLayout(FlowLayout.CENTER,10,30));
		
		createTags(label_tag);
		
		JLabel label_side = new JLabel();
		label_side.setPreferredSize(new Dimension(1000,550));
		label_side.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		
		label_menu = new JLabel();
		label_menu.setPreferredSize(new Dimension(1000,70));
		label_menu.setLayout(new FlowLayout(FlowLayout.CENTER,20,0));
		
		label_view = new JLabel();
		label_view.setPreferredSize(new Dimension(1000,480));
		label_view.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		
		label_side.add(label_menu);
		label_side.add(label_view);
	
		content = new ContentView(arra,task,label_background,label_menu, label_view,null,this);
		content.refreshView(category);
		
		label_content.add(label_tag);
		label_content.add(label_side);
		
	}
	
	
	public void createTags(JLabel label_tag)
	{
		tags = new Vector<JButton>();		
		
		for(int i=0; i<labels.length; i++)
		{
			JButton bt1 = new JButton();
			bt1.setPreferredSize(new Dimension(160,70));
			if(i==category)
				bt1.setIcon(new ImageIcon(dir+labels[i]+"_1.png"));
			else
				bt1.setIcon(new ImageIcon(dir+labels[i]+"_0.png"));
			bt1.addActionListener(this);
			bt1.setContentAreaFilled(false);
			bt1.setBorderPainted(false);
			bt1.setFocusPainted(false);
			bt1.setActionCommand(labels[i]);
			
			tags.add(bt1);			
			
			label_tag.add(tags.elementAt(i));
			label_tag.updateUI();
		}
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		System.out.println(e.getActionCommand());
		executeActions(e.getActionCommand());
	}
	
	public void executeActions(String action)
	{
		if(action.equals("CLOSE"))
		{
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			System.exit(0);
		}
		else if(action.equals("MINIMIZE"))
			this.setExtendedState(JFrame.ICONIFIED);
		
		for(int i=0 ; i<labels.length; i++)
		{
			if(action.equals(labels[i]))
			{
				category=i;
				changeView();
				task.changeFilter(category);
				
			}
		}
			
	}
	
	 public void changeView()
	 {
		 		 
		for(int i=0; i<labels.length; i++)
		{
			if(i==category)
				tags.elementAt(i).setIcon(new ImageIcon(dir+labels[i]+"_1.png"));
			else
				tags.elementAt(i).setIcon(new ImageIcon(dir+labels[i]+"_0.png"));
			tags.elementAt(i).updateUI();
		}
		
		content.refreshView(category);
		
		 
	 }
	
	 private void menuItemRestoreActionPerformed(java.awt.event.ActionEvent evt) 
	 {
	        this.setVisible(true);
	        this.toFront();
	        tray.remove(trayIcon);
	 }

	 private void menuItemCloseActionPerformed(java.awt.event.ActionEvent evt) 
	 {
	        System.exit(0);
	 }
	 
	 private void changeEvent(java.awt.event.WindowEvent evt) 
	 {
	        
		 if ( evt.getNewState() == ICONIFIED) 
		 {
	            this.setState(NORMAL);
	            this.setVisible(false);

	            if (SystemTray.isSupported()) 
	            {
	                
	                tray = SystemTray.getSystemTray();

	                MouseListener mouseListener = new MouseListener( ) 
	                {

	                    @Override
						public void mouseClicked(java.awt.event.MouseEvent e) 
	                    {
							if(e.getButton()==1)
	                    	{
							    setVisible(true);
						        toFront();
						        tray.remove(trayIcon);
	                    	}
							else if(e.getButton()==3)
							{
								popupContextual.setLocation(e.getX(), e.getY());
			                    popupContextual.setInvoker(popupContextual);
			                    popupContextual.setVisible(true);			                         
							}
							
						}

						@Override
						public void mouseEntered(java.awt.event.MouseEvent e) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void mouseExited(java.awt.event.MouseEvent e) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void mousePressed(java.awt.event.MouseEvent e) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void mouseReleased(java.awt.event.MouseEvent e) {
							// TODO Auto-generated method stub
							
						}
	                };

	                trayIcon.setImageAutoSize(true);
	                trayIcon.addMouseListener(mouseListener);
	                trayIcon.addActionListener( new ActionListener() 
	                {
	                        public void actionPerformed(ActionEvent e) 
	                        {
	                            menuItemRestoreActionPerformed(e);
	                        }
	                    }
	                );


	                try {
	                    tray.add(trayIcon);
	                } catch (AWTException e) {
	                    System.err.println("Error on Tray");
	                    this.setVisible(true);
	                }
	            } 
	            else 
	            {
	            }
	        }
	    }



	public void loadEvents(ArrayList<GenericEvent> events) {
		content.loadEvents(events);
	}
	
	public void actualizarTitulo(String name) {
		content.actualizaLetrero(name);
	}



	public void actualizaHeader(String dayName) {
		content.actualizaHeader(dayName);
	}
	public ContentView getCV()
    {
    	return content;
    }

}


